﻿using System;
using System.Collections;
using Gp.Scripts.GUI;
using Gp.Scripts.Core;
using UnityEngine;

namespace Gp.AI {
    public class AIActionArgs {
        public string Title { get; }
        public string Description { get; set; }
        public Sprite Icon { get; }
        public BaseUnit Unit { get; }

        public AIActionArgs(string title, string description, Sprite icon, BaseUnit unit) {
            Title = title;
            Description = description;
            Icon = icon;
            Unit = unit;
        }


        public static AIActionArgs Build(SkillEntity entity) {
            var data = entity.Config;
            // return new AIActionArgs(data.itemName, data.description, data.icon, entity.UnitReference);
            throw new NotImplementedException();
        }
    }
    
    
    
    public abstract class NormalAINode : AIActionNode { }


    public abstract class NormalAINodeEntry<T> : AINodeEntry<T> where T : NormalAINode {
        protected NormalAINodeEntry(T data) : base(data) { }
        private const float MinTime = 2f;

        public sealed override IEnumerator Execute() {
            if (Global.Get<BattleSystem>().IsUnitDestroyed(Unit)) yield break;
            PreCalculate();

            if (ShouldPerform()) {
                if (GetActionInfo() is {} info)
                    Global.Get<GridBattleGUISystem>().ShowAIAction(info);

                var time = Time.time;

                yield return OnExecute();
                
                // 最小执行时间
                var delta = Time.time - time;
                yield return new WaitForSeconds(Math.Max(0, MinTime - delta));
            }
            

            yield return ToNext(Data.Next);
        }
        
        
        protected virtual void PreCalculate() { }


        public virtual bool ShouldPerform() {
            return true;
        }
        
        public virtual IEnumerator OnExecute() {
            yield break;
        }


        protected virtual AIActionArgs GetActionInfo() {
            return null;
        }
    }

}